package com.bawei.module_goods.repository

import com.bawei.lib_net.RetrofitFactory
import com.bawei.module_goods.entity.AddCarParam
import com.bawei.module_goods.entity.ShoppingCartDisplayItem
import com.bawei.module_goods.entity.ShoppingCartItem
import com.bawei.module_goods.entity.UpdateCartItemRequest
import com.bawei.module_goods.network.GoodsApi

class ShoppingCartRepository {
    private val api = RetrofitFactory.getRetrofitFactory().create(GoodsApi::class.java)
    suspend fun getCartList(): List<ShoppingCartItem> {
        val resp = api.getShoppingCartList()
        return resp.data?.items ?: emptyList()
    }

    suspend fun getCartDisplayList(): List<ShoppingCartDisplayItem> {
        val cartList = getCartList()
        android.util.Log.d("ShoppingCart", "购物车原始数据: $cartList")
        val result = mutableListOf<ShoppingCartDisplayItem>()
        for (cart in cartList) {
            val detailResp = api.getGoodsDetail(cart.goodsId)
            val detail = detailResp.data
            android.util.Log.d("ShoppingCart", "商品详情: $detail")
            if (detail != null) {
                // 直接用DetailEntity字段，兜底用skuList第一个
                val skuFirst = detail.skuList?.firstOrNull()
                val imageUrl = detail.mainImgUrl ?: skuFirst?.skuImgUrl ?: ""
                val name = detail.title ?: skuFirst?.skuName ?: ""
                val price = detail.purchasePrice?.toDouble() ?: skuFirst?.purchasePrice?.toDouble() ?: 0.0
                result.add(
                    ShoppingCartDisplayItem(
                        cartId = cart.id,
                        goodsId = cart.goodsId,
                        goodsSkuId = cart.goodsSkuId,
                        number = cart.number ?: 1,
                        goodsName = name,
                        goodsImage = imageUrl,
                        price = price
                    )
                )
            }
        }
        android.util.Log.d("ShoppingCart", "合并后展示数据: $result")
        return result
    }

    suspend fun deleteCartItem(cartId: Int): Boolean {
        val resp = api.deleteCartItem(mapOf("carId" to cartId.toString()))
        return resp.data == true
    }

    suspend fun toggleCollect(goodsId: Int): Boolean {
        val resp = api.toggleCollect(goodsId)
        return resp.data == true
    }

    suspend fun updateCartItem(cartId: Int, number: Int): Boolean {
        val params = UpdateCartItemRequest(
            carId = cartId.toString(),
            number = number
        )
        android.util.Log.d("CartLog", "Repository updateCartItem params=$params")
        val resp = api.updateCartItem(params)
        android.util.Log.d("CartLog", "Repository updateCartItem resp=$resp")
        return resp.data == true
    }

    /*
      为什么这里会报错
     */
    suspend fun addToCart(goodsId: Int, skuId: Int, skuCode: String, skuName: String, number: Int): String {
        val addCar = AddCarParam(
            goodsCode = skuCode,
            goodsId = goodsId,
            goodsName = skuName,
            goodsSkuCode = skuCode,
            goodsSkuId = skuId,
            goodsSkuName = skuName,
            number = number
        )
        val resp = api.addToCart(addCar)
        return resp.data ?: ""

    }
} 